var app = getApp()
Page({
  data: {
		isUnfold:true,
    name: '',
    phone: '',
    wechatnum: ''
  },
  onLoad: function (options) {
    var that = this;
    
    if (!app.globalData.plumSession) {
      app.plumSessionCallback = plumSession => {
        that.requestPartners();
      }
    } else {
      that.requestPartners();
    }
  },
  onReady: function () {
  
  },
  onShow: function () {
    app.setNavtitle('成为分销商');
  },
  requestPartners: function () {
    var that = this;
    var data = {};
    data.map = 'applet_three_configure_new';
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            partners: res.data.data,
            isapply: res.data.data.isapply
          })

        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  nameChange:function(e){
    var that = this;
    that.setData({
      name:e.detail.value
    })
  },
  phoneChange: function (e) {
    var that = this;
    that.setData({
      phone: e.detail.value
    })
  },
  wechatnumChange: function (e) {
    var that = this;
    that.setData({
      wechatnum: e.detail.value
    })
  },
  submitRequest: function () {
    var that = this;
    app.getSubId(that, 'applet_three_apply_distribution_new', 'toSubmitRequest');
  },
  toSubmitRequest:function(e){
    var that = this;
    var data = {
      map:'applet_three_apply_distribution_new',
      realname: that.data.name,
      mobile: that.data.phone,
      wxno: that.data.wechatnum,
    }
    var hasname = that.data.partners.hasname;
    var hasphone = that.data.partners.hasphone;
    var haswx = that.data.partners.haswx;
    if (hasname==1){
      if (!data.realname) {
        app.errorTip(that, "请输入您的姓名", 2000);
        return;
      }
    }

    if (haswx == 1) {
      if (!data.wxno) {
        app.errorTip(that, "请输入您的微信号", 2000);
        return;
      }
    }
    console.log(data);
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          app.errorTip(that, res.data.data.msg, 2000);
          that.setData({
            name: '',
            phone: '',
            wechatnum: '',
            isapply: res.data.data.isapply
          })
          app.globalData.isdistrib = res.data.data.isdistrib;
          app.globalData.isapply = res.data.data.isapply;
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  }
})